package com.game.module.backpack.view
{
	import base.BaseHero;
	
	import com.dusk.util.ArrayUtil;
	import com.dusk.util.McUtil;
	import com.dusk.util.ResUtil;
	import com.dusk.util.TextUtil;
	import com.game.data.vo.EquipmentVO;
	import com.game.data.vo.PlayerDataVO;
	import com.game.data.vo.attribute.RoleAttribVO;
	import com.game.event.GameEventType;
	import com.game.manager.EventManager;
	import com.game.manager.NotificationManager;
	import com.game.module.backpack.vo.BackPackEvent;
	import com.game.view.component.icon.RoleEquipIcon;
	import com.game.view.layer.LayerBase;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class BackPack extends LayerBase
	{
		
		public function BackPack()
		{
			super();
		}
		private var _equipType:int = 0;
		private static const EQUIP_TYPE_1:int = 0;
		private static const EQUIP_TYPE_2:int = 1;
		
		//人物模型
		public var model:Sprite;
		public var btn_close:SimpleButton;
		public var mc_exp:MovieClip;
		public var txt_name:TextField;
		public var txt_power:TextField;
		public var txt_hp:TextField;
		public var txt_mp:TextField;
		public var txt_atk:TextField;
		public var txt_def:TextField;
		public var txt_crit:TextField;
		public var txt_miss:TextField;
		public var txt_hpHeal:TextField;
		public var txt_mpHeal:TextField;
		public var txt_lh:TextField;
		public var txt_exp:TextField;
		public var txt_hit:TextField;
		public var txt_magicDef:TextField;
		public var txt_toughness:TextField;
		public var txt_reMagicDef:TextField;
		public var pageIndex:TextField;
		
		public var packThing1:PackThings;
		public var packThing2:PackThings;
		public var packThing3:PackThings;
		public var packThing4:PackThings;
		
		public var btn_change:SimpleButton;
		
		public var elementTitle:BackPackElement;
		public var prePageBtn:SimpleButton;
		public var nextPageBtn:SimpleButton;
		public var sellMultiEquip:SimpleButton;
		public var isShowFashion:MovieClip;
		public var levelFigure:Sprite;
		
		private var headSprite:HeadSprite;
		private var hero:BaseHero;
		
		override protected function beforeAdded():void
		{
			var mc:MovieClip = ResUtil.getLayerView("BackPack") as MovieClip;
			TextUtil.redrawTxt(mc);
			model = mc.getChildByName("model") as Sprite;
			btn_close = mc.getChildByName("btn_close") as SimpleButton;
			mc_exp = mc.getChildByName("mc_exp") as MovieClip;
			mc_exp.gotoAndStop(1);
			txt_name = mc.getChildByName("txt_name") as TextField;
			txt_power = mc.getChildByName("txt_power") as TextField;
			txt_hp = mc.getChildByName("txt_hp") as TextField;
			txt_mp = mc.getChildByName("txt_mp") as TextField;
			txt_atk = mc.getChildByName("txt_atk") as TextField;
			txt_def = mc.getChildByName("txt_def") as TextField;
			txt_crit = mc.getChildByName("txt_crit") as TextField;
			txt_miss = mc.getChildByName("txt_miss") as TextField;
			txt_hpHeal = mc.getChildByName("txt_hpHeal") as TextField;
			txt_mpHeal = mc.getChildByName("txt_mpHeal") as TextField;
			txt_lh = mc.getChildByName("txt_lh") as TextField;
			txt_exp = mc.getChildByName("txt_exp") as TextField;
			txt_hit = mc.getChildByName("txt_hit") as TextField;
			txt_magicDef = mc.getChildByName("txt_magicDef") as TextField;
			txt_toughness = mc.getChildByName("txt_toughness") as TextField;
			txt_reMagicDef = mc.getChildByName("txt_reMagicDef") as TextField;
			pageIndex = mc.getChildByName("pageIndex") as TextField;
			
			packThing1 = new PackThings();
			packThing1.x = 168;
			packThing1.y = 170;
			packThing1.displayType = PackThings.DISPLAY_TYPE_EQUIP;
			mc.addChild(packThing1);
			packThing2 = new PackThings();
			packThing2.x = 420;
			packThing2.y = 170;
			packThing2.displayType = PackThings.DISPLAY_TYPE_EQUIP;
			mc.addChild(packThing2);
			packThing3 = new PackThings();
			packThing3.x = 168;
			packThing3.y = 243;
			packThing3.displayType = PackThings.DISPLAY_TYPE_EQUIP;
			mc.addChild(packThing3);
			packThing4 = new PackThings();
			packThing4.x = 420;
			packThing4.y = 243;
			packThing4.displayType = PackThings.DISPLAY_TYPE_EQUIP;
			mc.addChild(packThing4);
			btn_change = mc.getChildByName("btn_change") as SimpleButton;
			
			elementTitle = new BackPackElement();
			elementTitle.x = 516;
			elementTitle.y = 114;
			mc.addChild(elementTitle);
			prePageBtn = mc.getChildByName("prePageBtn") as SimpleButton;
			nextPageBtn = mc.getChildByName("nextPageBtn") as SimpleButton;
			sellMultiEquip = mc.getChildByName("sellMultiEquip") as SimpleButton;
			isShowFashion = mc.getChildByName("isShowFashion") as MovieClip;
			isShowFashion.buttonMode = true;
			levelFigure = mc.getChildByName("levelFigure") as Sprite;
			
			addChild(mc);
			setCloseBtn(btn_close);
		}
		
		override protected function onAdded():void
		{
			elementTitle.setPlayer(getPlayer());
			txt_name.addEventListener(FocusEvent.FOCUS_OUT, changeName);
			
			btn_change.addEventListener(MouseEvent.CLICK, changeEquipType);
			
			prePageBtn.addEventListener(MouseEvent.CLICK, changePage);
			nextPageBtn.addEventListener(MouseEvent.CLICK, changePage);
			changePage();
			sellMultiEquip.addEventListener(MouseEvent.CLICK, deleteMultiEquipment);
			isShowFashion.addEventListener(MouseEvent.CLICK, changeShowFashion);
			EventManager.getIns().registerListen(BackPackEvent.REFRESH_BACKPACK, refreshPanel);
			EventManager.getIns().registerListen(BackPackEvent.CLOSE_BACKPACK, removeFromParent);
			refreshPanel();
		}
		
		override protected function onRemoved():void
		{
			txt_name.removeEventListener(FocusEvent.FOCUS_OUT, changeName);
			
			btn_change.removeEventListener(MouseEvent.CLICK, changeEquipType);
			
			prePageBtn.removeEventListener(MouseEvent.CLICK, changePage);
			nextPageBtn.removeEventListener(MouseEvent.CLICK, changePage);
			sellMultiEquip.removeEventListener(MouseEvent.CLICK, deleteMultiEquipment);
			isShowFashion.removeEventListener(MouseEvent.CLICK, changeShowFashion);
			EventManager.getIns().removeListen(BackPackEvent.REFRESH_BACKPACK, refreshPanel);
			EventManager.getIns().removeListen(BackPackEvent.CLOSE_BACKPACK, removeFromParent);
			headSprite.removeFromParent();
			headSprite.destroy();
			headSprite = null;
			hero = null;
			EventManager.getIns().dispatchEvent(GameEventType.CONTINUE_GAME);
		}
		
		private function getHero():BaseHero
		{
			return hero;
		}
		
		public function getPlayer():PlayerDataVO
		{
			return getHero().getPlayer();
		}
		
		public function setPackOwner(owner:BaseHero):void
		{
			hero = owner as BaseHero;
		}
		
		public function setInfoTxt():void
		{
			var vo:RoleAttribVO = hero.roleProperties;
			refreshLvImage(vo.level);
			txt_name.text = getPlayer().playerName != "" ? getPlayer().playerName : getPlayer().getRoleName();
			mc_exp.gotoAndStop(Math.round(30 * vo.exp / vo.needExp));
			txt_power.text = getPower().toString();
			txt_hp.text = vo.hp + " / " + vo.hpMax;
			txt_mp.text = vo.mp + " / " + vo.mpMax;
			txt_exp.text = vo.exp + " / " + vo.needExp;
			txt_atk.text = vo.atk.toString();
			txt_def.text = vo.def.toString();
			txt_hpHeal.text = vo.hpHeal.toString();
			txt_mpHeal.text = vo.mpHeal.toString();
			txt_magicDef.text = vo.magicDef + " %";
			txt_hit.text = vo.hit + " %";
			txt_crit.text = vo.crit + " %";
			txt_miss.text = vo.miss + " %";
			txt_toughness.text = vo.toughness + " %";
			txt_reMagicDef.text = vo.deMagicDef + " %";
			txt_lh.text = getPlayer().lhValue.toString();
		}
		
		public function changeEquipType(evt:MouseEvent):void
		{
			_equipType = _equipType == EQUIP_TYPE_1 ? EQUIP_TYPE_2 : EQUIP_TYPE_1;
			refreshPanel();
		}
		
		/**
		 * 获取战斗力
		 * @return 战斗力
		 */
		private function getPower():int
		{
			var score:int = 0;
			var attribVO:RoleAttribVO = getHero().getAttribute();
			score += getPlayer().level * 2;
			score += attribVO.hpMax;
			score += attribVO.hpHeal * 2;
			score += attribVO.mpMax;
			score += attribVO.mpHeal * 2;
			score += attribVO.atk * 2;
			score += attribVO.def * 2;
			score += attribVO.crit * 2;
			score += attribVO.toughness * 2;
			score += attribVO.hit * 2;
			score += attribVO.miss * 2;
			score += attribVO.magicDef * 2;
			score += attribVO.deMagicDef * 2;
			score += attribVO.bloodThirst;
			score += attribVO.magicThirst;
			for each(var i:int in getPlayer().passiveSkill)
			{
				score += i * 10;
			}
			for each(var vo:EquipmentVO in getPlayer().wearEquip.getAllEquip())
			{
				score += vo.level * 5;
				score += vo.getPreciousValue() * vo.strengthLevel * 3;
			}
			return score;
		}
		
		/**
		 * 刷新等级图片
		 * @param level
		 */
		private function refreshLvImage(level:int):void
		{
			const interval:int = 26;
			const resName:String = "levelnum";
			var figureTexture:Bitmap = undefined;
			
			var bmContainer:Sprite = levelFigure.getChildByName(resName) != null ? (levelFigure.getChildByName(resName) as Sprite) : new Sprite();
			bmContainer.name = resName;
			if (!levelFigure.contains(bmContainer))
			{
				levelFigure.addChild(bmContainer);
			}
			McUtil.removeAllChildren(bmContainer);
			
			if (level.toString().length == 1)
			{
				figureTexture = ResUtil.getImageBitmap(resName + level);
				figureTexture.x = 21.8;
				figureTexture.y = 13;
				bmContainer.addChild(figureTexture);
			}
			else
			{
				for (var i:int = 0; i < level.toString().length; i++)
				{
					figureTexture = ResUtil.getImageBitmap(resName + level.toString().charAt(i));
					figureTexture.x = 5.8 + i * interval;
					figureTexture.y = 13;
					bmContainer.addChild(figureTexture);
				}
			}
		}
		
		/**
		 * 刷新面板
		 */
		public function refreshPanel():void
		{
			
			
			if(_equipType == EQUIP_TYPE_1)
			{
				packThing1.setIndicateText("武器");
				packThing2.setIndicateText("饰品");
				packThing3.setIndicateText("防具");
				packThing4.setIndicateText("法宝");
				packThing1.setEquipVO(getPlayer().wearEquip.weapon);
				packThing2.setEquipVO(getPlayer().wearEquip.decoration);
				packThing3.setEquipVO(getPlayer().wearEquip.cloth);
				packThing4.setEquipVO(getPlayer().wearEquip.attribMagicWeapon);
			}
			else if(_equipType == EQUIP_TYPE_2)
			{
				packThing1.setIndicateText("幻兵");
				packThing2.setIndicateText("头衔");
				packThing3.setIndicateText("时装");
				packThing4.setIndicateText("翅膀");
				packThing1.setEquipVO(getPlayer().wearEquip.fashionWeapon);
				packThing2.setEquipVO(getPlayer().wearEquip.title);
				packThing3.setEquipVO(getPlayer().wearEquip.fashionCloth);
				packThing4.setEquipVO(getPlayer().wearEquip.wing);
			}
			
			var showIdObj:Object = getPlayer().getEquipAppearance();
			getHero().refreshAppearance();
			if (headSprite)
			{
				headSprite.refreshEquip(showIdObj);
			}
			else
			{
				headSprite = new HeadSprite(getPlayer().roleId);
				headSprite.refreshEquip(showIdObj);
				model.addChild(headSprite);
			}
			isShowFashion.gotoAndStop(getPlayer().isShowFashion ? 2 : 1);
			setInfoTxt();
		}
		
		/**
		 * 改变名字
		 * @param evt 点击事件
		 */
		private function changeName(evt:FocusEvent):void
		{
			if (getPlayer().playerName != txt_name.text)
			{
				getPlayer().playerName = txt_name.text;
				NotificationManager.alert("昵称修改成功！");
			}
		}
		
		/**
		 * 改变装备页数
		 * @param evt 点击事件
		 */
		private function changePage(evt:MouseEvent = null):void
		{
			if (!evt)
			{
				elementTitle.curPage -= 1;
				return;
			}
			switch (evt.target)
			{
				case prePageBtn:
					elementTitle.curPage -= 1;
					break;
				case nextPageBtn:
					elementTitle.curPage += 1;
					break;
			}
			pageIndex.text = elementTitle.curPage.toString();
		}
		
		/**
		 * 删除普通装备
		 * @param evt 点击事件
		 */
		private function deleteMultiEquipment(evt:MouseEvent):void
		{
			var len:int = getPlayer().bagEquipList.length;
			while (len--)
			{
				var vo:EquipmentVO = getPlayer().bagEquipList[len];
				if (vo.quality == "普 通" && vo.type != "zbtx")
				{
					ArrayUtil.removeElement(getPlayer().bagEquipList, vo);
					getPlayer().lhValue += vo.getPreciousValue();
				}
			}
			EventManager.getIns().dispatchEvent(BackPackEvent.REFRESH_BACKPACK);
		}
		
		/**
		 * 改变是否显示时装
		 * @param evt 点击事件
		 */
		private function changeShowFashion(evt:MouseEvent):void
		{
			getPlayer().isShowFashion = !getPlayer().isShowFashion;
			refreshPanel();
		}
		
		/**
		 * 装备点击事件，主要处理脱下装备
		 * @param evt 点击事件
		 */
		private function equipClick(evt:MouseEvent):void
		{
			var pack:PackThings = evt.target as PackThings;
			if(!pack.getEquipVO())
			{
				return;
			}
			//法宝在使用中无法卸下
			if (evt.currentTarget.name == "zbfb")
			{
				if (getHero().getCurMagicWeapon() && getHero().getCurMagicWeapon().isUsing())
				{
					NotificationManager.alert("当前法宝正在使用");
					return;
				}
			}
			var icon:RoleEquipIcon = evt.currentTarget.getChildByName("curzb") as RoleEquipIcon;
			if (icon)
			{
				var vo:EquipmentVO = icon.getEquipVO();
				evt.currentTarget.removeChild(icon);
				getPlayer().wearEquip.takeOff(vo);
				//去除属性
				getHero().roleProperties.removeEquip(vo);
				//把vo放回背包内
				if (icon.getEquipVO().isFashion())
				{
					getPlayer().bugConsumeList.push(vo);
				}
				else
				{
					getPlayer().bagEquipList.push(vo);
				}
				
				refreshPanel();
				if (vo.type == "zbfb")
				{
					hero.changeMagicWeapon();
				}
				EventManager.getIns().dispatchEvent(BackPackEvent.REFRESH_BACKPACK);
			}
		}
	}
}
